﻿using System;
using System.ComponentModel;

using Infrastructure.NetExtensions;


namespace TempEmployees.Web
{
    public partial class DisplayTestboxOptionRB : BaseDisplayTextBox
    {
        public EventHandler OnTextChanged
        {
            set { DispTextBox.OnTextChanged = value; }
        }

        public override bool ReadOnly
        {
            set
            {
                if (value) PanelData.Visible = true;
                if (!value) SelectedIndexChanged();
                DispTextBox.ReadOnly = value;
                PanelRadioButtons.Visible = !value;
            }
            get
            {
                return DispTextBox.ReadOnly;
            }
        }

        [CategoryAttribute("Appearance")]
        public override string Text
        {
            set
            {
                DispTextBox.Text = value;
                RadioButtonSelectedIndex = 1;
            }
            get
            {
                return DispTextBox.Text;
            }
        }

        [CategoryAttribute("Appearance"),
         DescriptionAttribute("Extran info shown next to the text box"),
         DefaultValueAttribute("")]
        public string ExtraInfo
        {
            get { return DispTextBox.ExtraInfo; }
            set { DispTextBox.ExtraInfo = value; }
        }

        private const string DEFAULT_VALUE = "default_value";
        [CategoryAttribute("Misc")]
        public SelectedValueObject DefaultValue
        {
            set
            {
                ViewState.Set(DEFAULT_VALUE, value);
            }
            get
            {
                return ViewState.Get<SelectedValueObject>(DEFAULT_VALUE);
            }
        }

        public bool IsDefaultValue
        {
            set { RadioButtonListValueSelection.Items[0].Selected = value; }
            get { return RadioButtonListValueSelection.Items[0].Selected; }
        }

        public SelectedValueObject SelectedValue
        {
            get
            {
                return (IsDefaultValue) ? DefaultValue : (SelectedValueObject)DispTextBox.Text;
            }
        }

        [CategoryAttribute("Appearance")]
        public string DefaultRadioButtonText
        {
            set { RadioButtonListValueSelection.Items[0].Text = value; }
            get { return RadioButtonListValueSelection.Items[0].Text; }
        }

        [CategoryAttribute("Appearance")]
        public string OptionRadioButtonText
        {
            set { RadioButtonListValueSelection.Items[1].Text = value; }
            get { return RadioButtonListValueSelection.Items[1].Text; }
        }

        public override short TabIndex
        {
            get { return DispTextBox.TabIndex; }
            set { DispTextBox.TabIndex = value; }
        }

        protected int RadioButtonSelectedIndex
        {
            set
            {
                RadioButtonListValueSelection.SelectedIndex = value;
                SelectedIndexChanged();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadioButtonSelectedIndex = 0;

                DispTextBox.Text = DefaultValue;
            }
        }

        public override void Reset()
        {
            DispTextBox.Reset();
        }

        protected void RadioButtonListValueSelection_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged();
        }

        public override bool IsFilled()
        {
            return (RadioButtonListValueSelection.Items[0].Selected || DispTextBox.IsFilled());
        }

        public override bool IsValid()
        {
            DispTextBox.ValidationType = ValidationType;
            return RadioButtonListValueSelection.Items[0].Selected || DispTextBox.IsValid();
        }

        protected void SelectedIndexChanged()
        {
            bool isDefault = (RadioButtonListValueSelection.SelectedIndex == 0);
            PanelData.Visible = !isDefault;
        }
    }
}